package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllStandards", query = "select myStandard from Standard myStandard"),
		@NamedQuery(name = "findStandardByName", query = "select myStandard from Standard myStandard where myStandard.name = ?1"),
		@NamedQuery(name = "findStandardByNameContaining", query = "select myStandard from Standard myStandard where myStandard.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findStandardByNumber", query = "select myStandard from Standard myStandard where myStandard.number = ?1"),
		@NamedQuery(name = "findStandardByNumberContaining", query = "select myStandard from Standard myStandard where myStandard.number like concat('%',?1,'%')"),
		@NamedQuery(name = "findStandardByEffectivedate", query = "select myStandard from Standard myStandard where myStandard.effectivedate = ?1"),
		@NamedQuery(name = "findStandardByExpirydate", query = "select myStandard from Standard myStandard where myStandard.expirydate = ?1"),
		@NamedQuery(name = "findStandardByDownurl", query = "select myStandard from Standard myStandard where myStandard.downurl = ?1"),
		@NamedQuery(name = "findStandardByDownurlContaining", query = "select myStandard from Standard myStandard where myStandard.downurl like concat('%',?1,'%')"),
		@NamedQuery(name = "findStandardByDescription", query = "select myStandard from Standard myStandard where myStandard.description = ?1"),
		@NamedQuery(name = "findStandardByDescriptionContaining", query = "select myStandard from Standard myStandard where myStandard.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllStandardsByReplacedstandard", query = "select myStandard from Standard myStandard where myStandard.replacedstandard.id = ?1"),
		@NamedQuery(name = "findAllStandardsByStandardtype", query = "select myStandard from Standard myStandard where myStandard.standardtype.id = ?1"),
		@NamedQuery(name = "findAllStandardsByStandardlevel", query = "select myStandard from Standard myStandard where myStandard.standardlevel.id = ?1"),
		@NamedQuery(name = "findStandardById", query = "select myStandard from Standard myStandard where myStandard.id = ?1"),
		@NamedQuery(name = "findStandardByPrimaryKey", query = "select myStandard from Standard myStandard where myStandard.id = ?1 "),
		@NamedQuery(name = "deleteStandardsByIds", query = "delete Standard where id in (?1)"),
		@NamedQuery(name = "findStandardsByIds", query = "select myStandard from Standard myStandard where myStandard.id in (?1)"),
		@NamedQuery(name = "findStandardByPhyscialPrimaryKey", query = "select myStandard from Standard myStandard where  myStandard.number = ?1"),
		@NamedQuery(name = "countStandardsByName", query = "select count(myStandard)  from Standard myStandard where myStandard.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "standard")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Standard")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Standard implements Serializable, Comparable<Standard> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "number")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String number;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "effectivedate")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar effectivedate;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "expirydate")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar expirydate;

	@Column(name = "downurl")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String downurl;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "replacedstandard", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Standard replacedstandard;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "standardtype", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Standardtype standardtype;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "standardlevel", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Standardlevel standardlevel;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "standard", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Featurestandard> featurestandardsForStandard;
	@JSONField(serialize = false)
	@ManyToMany(mappedBy = "standardsForFoodtype", fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodtype> foodtypesForStandard;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "replacedstandard", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Standard> standardsForReplacedstandard;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getNumber() {
		return this.number;
	}

	public void setEffectivedate(Calendar effectivedate) {
		this.effectivedate = effectivedate;
	}

	public Calendar getEffectivedate() {
		return this.effectivedate;
	}

	public void setExpirydate(Calendar expirydate) {
		this.expirydate = expirydate;
	}

	public Calendar getExpirydate() {
		return this.expirydate;
	}

	public void setDownurl(String downurl) {
		this.downurl = downurl;
	}

	public String getDownurl() {
		return this.downurl;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setReplacedstandard(Standard replacedstandard) {
		this.replacedstandard = replacedstandard;
	}

	public Standard getReplacedstandard() {
		return replacedstandard;
	}

	public void setStandardtype(Standardtype standardtype) {
		this.standardtype = standardtype;
	}

	public Standardtype getStandardtype() {
		return standardtype;
	}

	public void setStandardlevel(Standardlevel standardlevel) {
		this.standardlevel = standardlevel;
	}

	public Standardlevel getStandardlevel() {
		return standardlevel;
	}

	public void setFeaturestandardsForStandard(Set<Featurestandard> featurestandardsForStandard) {
		this.featurestandardsForStandard = featurestandardsForStandard;
	}

	public Set<Featurestandard> getFeaturestandardsForStandard() {
		if (featurestandardsForStandard == null) {
			featurestandardsForStandard = new java.util.LinkedHashSet<Featurestandard>();
		}
		return featurestandardsForStandard;
	}

	public void setFoodtypesForStandard(Set<Foodtype> foodtypesForStandard) {
		this.foodtypesForStandard = foodtypesForStandard;
	}

	public Set<Foodtype> getFoodtypesForStandard() {
		if (foodtypesForStandard == null) {
			foodtypesForStandard = new java.util.LinkedHashSet<Foodtype>();
		}
		return foodtypesForStandard;
	}

	public void setStandardsForReplacedstandard(Set<Standard> standardsForReplacedstandard) {
		this.standardsForReplacedstandard = standardsForReplacedstandard;
	}

	public Set<Standard> getStandardsForReplacedstandard() {
		if (standardsForReplacedstandard == null) {
			standardsForReplacedstandard = new java.util.LinkedHashSet<Standard>();
		}
		return standardsForReplacedstandard;
	}

	public void copy(Standard that) {

		setId(that.getId());

		setName(that.getName());
		setNumber(that.getNumber());
		setEffectivedate(that.getEffectivedate());
		setExpirydate(that.getExpirydate());
		setDownurl(that.getDownurl());
		setDescription(that.getDescription());

		setReplacedstandard(that.getReplacedstandard());

		setStandardtype(that.getStandardtype());

		setStandardlevel(that.getStandardlevel());

		setFeaturestandardsForStandard(new java.util.LinkedHashSet<Featurestandard>(
				that.getFeaturestandardsForStandard()));

		setFoodtypesForStandard(
				new java.util.LinkedHashSet<Foodtype>(that.getFoodtypesForStandard()));

		setStandardsForReplacedstandard(new java.util.LinkedHashSet<Standard>(
				that.getStandardsForReplacedstandard()));

	}

	public Standard() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("number=[").append(number).append("] ");
		buffer.append("effectivedate=[").append(effectivedate).append("] ");
		buffer.append("expirydate=[").append(expirydate).append("] ");
		buffer.append("downurl=[").append(downurl).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Standard))
			return false;
		Standard comparedStandard = (Standard) obj;
		if (!CheckPropertyEqual(this.number, comparedStandard.number)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Standard o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
